package kr.hyunist.androidcomplete01saeildamil.ch12;

import java.text.*;
import java.util.*;

import kr.hyunist.androidcomplete01saeildamil.*;
import android.app.*;
import android.content.*;
import android.graphics.*;
import android.os.*;
import android.view.*;
import android.view.View.OnLongClickListener;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

public class CustomAdapterEx extends Activity {
	ArrayList<MemoListInfo> arList;
	MemoListInfo mInfo;
	MemoListAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.customadapterex);

		arList = new ArrayList<MemoListInfo>();

		String title;
		String subtitle;
		Date date = new Date();

		for (int i = 0; i < 10; i++) {
			title = "Title" + i;
			subtitle = "Sub title" + i;
			mInfo = new MemoListInfo(title, subtitle, date.getTime());
			arList.add(mInfo);
		}

		adapter = new MemoListAdapter(CustomAdapterEx.this,
				R.layout.customeadapterexcontent, arList);
		final ListView list = (ListView) findViewById(R.id.customeadapterexlistview);
		list.setAdapter(adapter);
		list.setOnItemLongClickListener(mOnLongClickListener);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.list_menu, menu);
		return true;
	}

	public void deleteClick(MenuItem item){
		arList.clear();
		adapter.notifyDataSetChanged();
	}
	
	OnItemLongClickListener mOnLongClickListener = new OnItemLongClickListener() {

		@Override
		public boolean onItemLongClick(AdapterView<?> parent, View view,
				int position, long id) {
			arList.remove(position);
			adapter.notifyDataSetChanged();
			Toast.makeText(CustomAdapterEx.this, "삭제위치 : " + position,
					Toast.LENGTH_SHORT).show();
			return true;
		}

	};
}

class MemoListInfo {
	String title;
	String subtitle;
	long date;

	public MemoListInfo(String title, String subtitle, long date) {
		super();
		this.title = title;
		this.subtitle = subtitle;
		this.date = date;
	}

	public String getTitle() {
		return title;
	}

	public String getSubtitle() {
		return subtitle;
	}

	public long getDate() {
		return date;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setSubtitle(String subtitle) {
		this.subtitle = subtitle;
	}

	public void setDate(long date) {
		this.date = date;
	}

}

class MemoListAdapter extends BaseAdapter {
	ArrayList<MemoListInfo> arMemo;
	Context mainCon;
	int layout;
	LayoutInflater inflater;
	String[] mColor = { "#90ffA07A", "#90ADFF2F", "#90FF8C00", "#90FFFF00",
			"#90AFEEEE" };

	public MemoListAdapter(Context context, int mLayout,
			ArrayList<MemoListInfo> arList) {
		mainCon = context;
		layout = mLayout;
		arMemo = arList;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return arMemo.size();
	}

	@Override
	public Object getItem(int position) {
		return arMemo.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final int pos = position;

		if (convertView == null) {
			convertView = inflater.inflate(layout, parent, false);
		}

		convertView.setBackgroundColor(Color.parseColor(mColor[pos % mColor.length]));

		TextView title = (TextView) convertView
				.findViewById(R.id.customadapterextitletv1);
		title.setText(arMemo.get(pos).getTitle());

		TextView content = (TextView) convertView
				.findViewById(R.id.customadapterexcontenttv2);
		content.setText(arMemo.get(pos).getSubtitle());

		SimpleDateFormat fm = new SimpleDateFormat("yyyy/MM/dd hh:m a");
		Date d = new Date();
		d.setTime(arMemo.get(pos).getDate());

		TextView date = (TextView) convertView
				.findViewById(R.id.customadapterextimetv3);
		date.setText(fm.format(d));

		return convertView;
	}

}
